package com.chenjj.java.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy implements InvocationHandler {

    private Object target = null;

    public <T> T band(T target){
        this.target = target;
        return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
       Object result = null;
       if (target == null){
           throw new Exception("JdkProxy not bind target(realSubject)");
       }
        System.out.println("before invoke");
        result = method.invoke(target,objects);
        System.out.println("after invoke");
        return result;
    }
}
